package org.mineground.handlers;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.bukkit.configuration.file.FileConfiguration;
import org.mineground.Main;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @file ConfigHandler.java (22.01.2012)
 * @author Daniel Koenen
 *
 */
public class ConfigHandler {
    private static final Logger ExceptionLogger = LoggerFactory.getLogger(ConfigHandler.class);
    
    private FileConfiguration configHandler;
    
    private boolean liveVersion;

    private String ircBotName;
    private String ircNetwork;
    private String ircBindAddr;
    private String ircChannel;
    private String ircDevChannel;
    private String ircDevPassword;
    private char ircCommandPrefix;
    private int ircPort;

    private String mySQLHost;
    private String mySQLUser;
    private String mySQLPassword;
    private String mySQLDatabase;
    private int mySQLPort;

    private String adminChatPrefix;
    private String groupChatPrefix;
    
    private String mainWorldName;
    private String survivalWorldName;
    private String survivalNetherWorldName;
    private String creativeWorldName;
    private String classicWorldName;

    private List<String> reactionBlockName = new ArrayList<String>();
    private List<Integer> reactionBlockId = new ArrayList<Integer>();
    private List<Integer> reactionBlockAmount = new ArrayList<Integer>();
    
    private int reactionTestDelay;
    private int announceMessageDelay;
    private int timeDelay;
    
    public ConfigHandler(FileConfiguration configFile) {
        configHandler = configFile;
        readConfig();
    }
    
    private void readConfig() {
        liveVersion = configHandler.getBoolean("Options.liveVersion", false);
	adminChatPrefix = configHandler.getString("Options.adminChatPrefix", "@");
	groupChatPrefix = configHandler.getString("Options.groupChatPrefix", "#");
        
        ircBotName = configHandler.getString("IRC.botName", "Gunther");
	ircNetwork = configHandler.getString("IRC.network", "irc.gtanet.com");
	ircBindAddr = configHandler.getString("IRC.bindAddress", "127.0.0.1");
	ircChannel = (liveVersion) ? (configHandler.getString("IRC.echoChannel", "#LVP.Minecraft")) : (configHandler.getString("IRC.testChannel", "#LVP.Minecraft2"));
	ircDevChannel = configHandler.getString("IRC.devChannel", "#LVP.Minecraft.Dev");
	ircDevPassword = configHandler.getString("IRC.devPassword", "bla");
	ircCommandPrefix = configHandler.getString("IRC.commandPrefix", "!").charAt(0);
	ircPort = configHandler.getInt("IRC.port", 6667);

	mySQLHost = configHandler.getString("MySQL.host", "localhost");
	mySQLUser = configHandler.getString("MySQL.user", "root");
	mySQLPassword = configHandler.getString("MySQL.password", "");
	mySQLDatabase = configHandler.getString("MySQL.database", "mineground");
	mySQLPort = configHandler.getInt("MySQL.port", 3306);
        
        mainWorldName = configHandler.getString("World.mainName", "main");
        survivalWorldName = configHandler.getString("World.survivalName", "survival");
        survivalNetherWorldName = configHandler.getString("World.survivalNetherName", "survival_nether");
        creativeWorldName = configHandler.getString("World.creativeName", "creative");
        classicWorldName = configHandler.getString("World.classicName", "classic");
        
        List<String> reactionPrice = configHandler.getStringList("Reactiontest.Prices");
	Iterator<String> iterator;

	iterator = reactionPrice.iterator();
	while (iterator.hasNext()) {
	    String[] values = iterator.next().toString().split(",");

            reactionBlockName.add(values[0]);
            reactionBlockId.add(Integer.parseInt(values[1]));
            reactionBlockAmount.add(Integer.parseInt(values[2]));
	}

	reactionTestDelay = configHandler.getInt("Reactiontest.delay", 5);
        announceMessageDelay = configHandler.getInt("AnnounceMessage.delay", 2);
        timeDelay = configHandler.getInt("Time.delay", 10);
    }
    
    public boolean writeDefaultConfig() {
        configHandler.set("Options.liveVersion", false);
        configHandler.set("Options.adminChatPrefix", "@");
        configHandler.set("Options.groupChatPrefix", "#");
        
        configHandler.set("IRC.botName", "Gunther");
	configHandler.set("IRC.network", "irc.gtanet.com");
	configHandler.set("IRC.bindAddress", "127.0.0.1");
	configHandler.set("IRC.testChannel", "#LVP.Minecraft2");
	configHandler.set("IRC.devChannel", "#LVP.Minecraft.Dev");
	configHandler.set("IRC.devPassword", "bla");
	configHandler.set("IRC.commandPrefix", "!");
	configHandler.set("IRC.port", 6667);
        
        configHandler.set("MySQL.host", "localhost");
	configHandler.set("MySQL.user", "root");
	configHandler.set("MySQL.password", "");
	configHandler.set("MySQL.database", "mineground");
	configHandler.set("MySQL.port", 3306);
        
        configHandler.set("World.mainName", "main");
        configHandler.set("World.survivalName", "survival");
        configHandler.set("World.creativeName", "creative");
        configHandler.set("World.oldName", "old");
        
        configHandler.set("Reactiontest.delay", 5);
        configHandler.set("Reactiontest.Prices", "Format: blockname,blockid,amountofblocks");
        
        try {
            configHandler.save(new File(Main.getInstance().getDataFolder(), "config.yml"));
            return true;
        }
        
        catch (Exception exception) {
            ExceptionLogger.error("Exception caught", exception);
            return false;
        }
    }

	public FileConfiguration getConfigHandler() {
		return configHandler;
	}

	public void setConfigHandler(FileConfiguration configHandler) {
		this.configHandler = configHandler;
	}

	public boolean isLiveVersion() {
		return liveVersion;
	}

	public void setLiveVersion(boolean liveVersion) {
		this.liveVersion = liveVersion;
	}

	public String getIrcBotName() {
		return ircBotName;
	}

	public void setIrcBotName(String ircBotName) {
		this.ircBotName = ircBotName;
	}

	public String getIrcNetwork() {
		return ircNetwork;
	}

	public void setIrcNetwork(String ircNetwork) {
		this.ircNetwork = ircNetwork;
	}

	public String getIrcBindAddr() {
		return ircBindAddr;
	}

	public void setIrcBindAddr(String ircBindAddr) {
		this.ircBindAddr = ircBindAddr;
	}

	public String getIrcChannel() {
		return ircChannel;
	}

	public void setIrcChannel(String ircChannel) {
		this.ircChannel = ircChannel;
	}

	public String getIrcDevChannel() {
		return ircDevChannel;
	}

	public void setIrcDevChannel(String ircDevChannel) {
		this.ircDevChannel = ircDevChannel;
	}

	public String getIrcDevPassword() {
		return ircDevPassword;
	}

	public void setIrcDevPassword(String ircDevPassword) {
		this.ircDevPassword = ircDevPassword;
	}

	public char getIrcCommandPrefix() {
		return ircCommandPrefix;
	}

	public void setIrcCommandPrefix(char ircCommandPrefix) {
		this.ircCommandPrefix = ircCommandPrefix;
	}

	public int getIrcPort() {
		return ircPort;
	}

	public void setIrcPort(int ircPort) {
		this.ircPort = ircPort;
	}

	public String getMySQLHost() {
		return mySQLHost;
	}

	public void setMySQLHost(String mySQLHost) {
		this.mySQLHost = mySQLHost;
	}

	public String getMySQLUser() {
		return mySQLUser;
	}

	public void setMySQLUser(String mySQLUser) {
		this.mySQLUser = mySQLUser;
	}

	public String getMySQLPassword() {
		return mySQLPassword;
	}

	public void setMySQLPassword(String mySQLPassword) {
		this.mySQLPassword = mySQLPassword;
	}

	public String getMySQLDatabase() {
		return mySQLDatabase;
	}

	public void setMySQLDatabase(String mySQLDatabase) {
		this.mySQLDatabase = mySQLDatabase;
	}

	public int getMySQLPort() {
		return mySQLPort;
	}

	public void setMySQLPort(int mySQLPort) {
		this.mySQLPort = mySQLPort;
	}

	public String getAdminChatPrefix() {
		return adminChatPrefix;
	}

	public void setAdminChatPrefix(String adminChatPrefix) {
		this.adminChatPrefix = adminChatPrefix;
	}

	public String getGroupChatPrefix() {
		return groupChatPrefix;
	}

	public void setGroupChatPrefix(String groupChatPrefix) {
		this.groupChatPrefix = groupChatPrefix;
	}

	public String getMainWorldName() {
		return mainWorldName;
	}

	public void setMainWorldName(String mainWorldName) {
		this.mainWorldName = mainWorldName;
	}

	public String getSurvivalWorldName() {
		return survivalWorldName;
	}

	public void setSurvivalWorldName(String survivalWorldName) {
		this.survivalWorldName = survivalWorldName;
	}

	public String getSurvivalNetherWorldName() {
		return survivalNetherWorldName;
	}

	public void setSurvivalNetherWorldName(String survivalNetherWorldName) {
		this.survivalNetherWorldName = survivalNetherWorldName;
	}

	public String getCreativeWorldName() {
		return creativeWorldName;
	}

	public void setCreativeWorldName(String creativeWorldName) {
		this.creativeWorldName = creativeWorldName;
	}

	public String getClassicWorldName() {
		return classicWorldName;
	}

	public void setClassicWorldName(String classicWorldName) {
		this.classicWorldName = classicWorldName;
	}

	public List<String> getReactionBlockName() {
		return reactionBlockName;
	}

	public void setReactionBlockName(List<String> reactionBlockName) {
		this.reactionBlockName = reactionBlockName;
	}

	public List<Integer> getReactionBlockId() {
		return reactionBlockId;
	}

	public void setReactionBlockId(List<Integer> reactionBlockId) {
		this.reactionBlockId = reactionBlockId;
	}

	public List<Integer> getReactionBlockAmount() {
		return reactionBlockAmount;
	}

	public void setReactionBlockAmount(List<Integer> reactionBlockAmount) {
		this.reactionBlockAmount = reactionBlockAmount;
	}

	public int getReactionTestDelay() {
		return reactionTestDelay;
	}

	public void setReactionTestDelay(int reactionTestDelay) {
		this.reactionTestDelay = reactionTestDelay;
	}

	public int getAnnounceMessageDelay() {
		return announceMessageDelay;
	}

	public void setAnnounceMessageDelay(int announceMessageDelay) {
		this.announceMessageDelay = announceMessageDelay;
	}

	public int getTimeDelay() {
		return timeDelay;
	}

	public void setTimeDelay(int timeDelay) {
		this.timeDelay = timeDelay;
	}

	public static Logger getExceptionlogger() {
		return ExceptionLogger;
	}
}